/*
 * This file is part of jboss-admin-rest
 * Copyright 2009, Christos Vasilakis (cvasilak@gmail.com)
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.jboss.admin.rest.services;

import org.jboss.admin.rest.domain.ServerInfoBean;
import org.jboss.admin.rest.exceptions.InternalServerErrorException;
import org.jboss.admin.rest.factory.ProfileServiceFactory;
import org.jboss.deployers.spi.management.ManagementView;
import org.jboss.managed.api.ComponentType;
import org.jboss.managed.api.ManagedComponent;
import org.jboss.resteasy.annotations.providers.jaxb.Formatted;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;

@Produces( {"application/xml", "application/json"} )  
public class ServerInfoResource {

	@GET
	@Formatted
	public ServerInfoBean getServerInfo() throws InternalServerErrorException {
		ManagementView mv = ProfileServiceFactory.getCurrentManagementView();
		ServerInfoBean info = null;

		try {
			ManagedComponent serverInfo = mv.getComponent("jboss.system:type=ServerInfo", new ComponentType("MCBean", "ServerInfo"));
			ManagedComponent server = mv.getComponent("jboss.system:type=MCServer", new ComponentType("MCBean", "MCServer"));
			ManagedComponent serverConfig = mv.getComponent("jboss.system:type=ServerConfig", new ComponentType("MCBean", "ServerConfig"));
			ManagedComponent jta = mv.getComponent("TransactionStatistics", new ComponentType("MCBean", "JTA"));

			info = new ServerInfoBean(serverInfo, server, serverConfig, jta);

		} catch (Exception e) {
			throw new InternalServerErrorException(e.getMessage());
		}

		return info;
	}
}
